package com.jxmcloud.business.cut.service.impl.logic;

import com.jxmcloud.business.cut.json.DataConfigActivityStartLoader;
import com.jxmcloud.business.cut.json.DataConfigStartLoader;
import com.jxmcloud.business.cut.logic.DataConfigActionLoader;
import com.jxmcloud.business.cut.model.database.DBUserActivityInfo;
import com.jxmcloud.business.cut.service.database.IUserActivityInfoDBService;
import com.jxmcloud.business.cut.service.logic.IConfigActionUtil;
import com.jxmcloud.business.cut.util.MyTool;
import com.jxmcloud.business.cut.vo.ActivityVo;
import com.jxmcloud.business.cut.vo.json.ConfigActivityStartVo;
import com.jxmcloud.business.cut.vo.json.ConfigAwardVo;
import com.jxmcloud.business.cut.vo.json.ConfigStartVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ConfigActionUtilImpl implements IConfigActionUtil {
    @Resource
    private DataConfigStartLoader dataConfigStartLoader;
    @Resource
    private DataConfigActivityStartLoader dataConfigActivityStartLoader;
    @Resource
    private DataConfigActionLoader dataConfigActionLoader;
    @Resource
    private IUserActivityInfoDBService userActivityInfoDBService;

    @Override
    public void initAllConfig() {
        dataConfigActionLoader.initAllConfig();
        // 加载加密配置
        MyTool.makeNewIDes();
    }

    @Override
    public List<ConfigAwardVo> getAwardPoolById(Integer poolId) {
        return dataConfigActionLoader.getAwardPoolById(poolId);
    }

    /**
     * 从当前日期中随机获取一个活动
     * @param day
     * @return
     */
    @Override
    public ActivityVo getRandomActivity(Integer uid, String day) {
        List<DBUserActivityInfo> list = userActivityInfoDBService.lambdaQuery().eq(DBUserActivityInfo::getUid, uid).list();
        Set<Integer> joinedActivityIds = list.stream().map(DBUserActivityInfo::getActivityId).collect(Collectors.toSet());

        Map<Integer, ConfigActivityStartVo> configActivityStartMap = dataConfigActivityStartLoader.getConfigActivityStartMap();
        // 筛选符合时间条件的活动配置
        List<ConfigActivityStartVo> validActivities = configActivityStartMap.values().stream()
                .filter(vo -> !joinedActivityIds.contains(vo.getId())
                        && day.compareTo(vo.getStartTime()) >= 0 && (vo.getEndTime() == null || day.compareTo(vo.getEndTime()) <= 0))
                .collect(Collectors.toList());

        // 根据用户活动信息过滤活动

        Map<Integer, ConfigStartVo> configStartMap = dataConfigStartLoader.getConfigStartMap();

        if (!validActivities.isEmpty()) {
            ConfigActivityStartVo selected = validActivities.get(new Random().nextInt(validActivities.size()));

            // 遍历configStartMap查找关联配置
            ConfigStartVo matchedConfig = configStartMap.values().stream()
                    .filter(vo -> vo.getActivityId().equals(selected.getId()))
                    .findFirst()
                    .orElse(null);

            if (matchedConfig != null) {
                ActivityVo activityVo = new ActivityVo();
                activityVo.setId(selected.getId());
                activityVo.setStartId(matchedConfig.getId());
                activityVo.setStartTime(selected.getStartTime());
                activityVo.setEndTime(selected.getEndTime());
                activityVo.setNext(matchedConfig.getNext());
                return activityVo;
            }
        }
        return null;
    }

}
